'use strict'

/*返回一个虚拟DOM*/
function createDOM(tag = 'div') {
  return document.createDocumentFragment()
}



/**
 * 根据id返回一个 dom对象或者多个对象的集合
 *
 * @param      {str}      id      合法的元素id
 * @return     {boolean}  { description_of_the_return_value }
 */
function $(id, Element = false) {
  if (id) {
    let tar = id.toString().substring(1)
    switch (id.charAt(0)) {
      case '.':
        return Element ? Element.getElementsByClassName(tar) : document.getElementsByClassName(tar);
        break;

      case '#':
        return Element ? Element.getElementById(tar) : document.getElementById(tar);
        break;

      default:
        return Element ? Element.getElementsByTagName(id) : document.getElementsByTagName(id);
        break;
    }
  } else {
    console.log('id需要是字符串');
  }
}

function checkDir(tarPath) {
  if (!fs.existsSync(tarPath)) return fs.mkdirSync(tarPath);
}

/**
 * 通过遍历,往上层查找对应clasname的父元素
 *
 * @param      {元素标签}  Element   The element
 * @param      {<string>}  findName  The find name
 * @param      {递归次数}  T         { parameter_description }
 * @return     {<type>}  The parent by class name.
 */
function getParentByClassName(Element, findName, T = 5) {
  if (T <= 0) return
  // console.log('开始查找:' + findName)
  let parent = Element.parentElement
  if (parent) {
    if (parent.className == findName) {
      return parent
    }
    T = T - 1
    return getParentByClassName(parent, findName, T)
  }
}



/**
 * 判断是否数组
 *
 * @param      {<type>}   tar     The tar
 * @return     {boolean}  True if array, False otherwise.
 */
function isArray(tar) {
  return typeof(tar) === 'undefined' ? false : tar instanceof Array;
}

/**
 * 直接打印目标对象，如果是数组，自动遍历
 *
 * @param      {<type>}  tar     The tar
 */
function print(tar) {
  console.log('typeof:', typeof(tar));
  if (tar instanceof Array) {
    console.log('当前是数组');
    let num = 0
    for (let each of tar) {
      console.log(num + '、' + each);
      num++
    }
    console.log(`共有${tar.length}个元素`);
  } else {
    console.log(tar);
  }
}

function addElementByObject(parent, children, style) {
  children.forEach(function(element, index) {
    console.log(`创建${element.el},class：${element.class},id:${element.id}`)
  });
}

/**
 * 输入一个元素，在此元素下创建新的子元素
 *
 * @param      {<type>}  doc      父节点
 * @param      {<type>}  htmlTag  要创建的标签类型
 * @param      {<type>}  value    对应元素的内容
 * @return     {<type>}  { description_of_the_return_value }
 */
function addElement(htmlTag, style = {}) {
  let newTag = document.createElement(htmlTag);
  if (style) {
    Object.assign(newTag, style);
  }
  return newTag;
}

/**
 * 创建定时器，并添加移出条件
 *
 * @param      {Function}  callback  The callback
 * @param      {<type>}    delay     The delay
 */
function interval(delay, callback) {
  let id = setInterval(() => callback(id), delay);
  return id
}

/**
 * 使用apply 将数组2 合并到数组1里面，并清空数组2
 *
 * @param      {Array}   a1      A 1
 * @param      {<type>}  a2      A 2
 */
function mergeArray(a1, a2) {
  let len = a1.length;
  if (len < 4000) {
    a1.push.apply(a1, a2)
    a2.pop()
    // a2.unshift.apply(a2, a1)
  } else {
    for (let i = 0; i < len; i = i + 5000) {
      a2.unshift.apply(a2, a1.slice(i, i + 5000));
    }
  }
}

/**
 * Gets the file size.
 *
 * @param      {number}  total   The total
 * @param      {string}  unit    The unit
 * @return     {number}  The file size.
 */
function getFileSize(fileSize) {
  if (fileSize < 1024) {
    return fileSize + 'B';
  } else if (fileSize < (1024 * 1024)) {
    let temp = fileSize / 1024;
    temp = temp.toFixed(2);
    return temp + 'KB';
  } else if (fileSize < (1024 * 1024 * 1024)) {
    let temp = fileSize / (1024 * 1024);
    temp = temp.toFixed(2);
    return temp + 'MB';
  } else {
    let temp = fileSize / (1024 * 1024 * 1024);
    temp = temp.toFixed(2);
    return temp + 'GB';
  }
}

/**
 * Gets the file size.
 *
 * @param      {number}  total   The total
 * @param      {string}  unit    The unit
 * @return     {number}  The file size.
 */
function converSize(total, unit = 'KB') {
  unit = unit.toUpperCase(unit)
  let format;
  let len = total / (1024.0);
  if (len > 1000) {
    return arguments.callee(len, ++n);
  } else {
    switch (unit) {
      case "KB":
        format = len.toFixed(2) + "KB";
        break;
      case "MB":
        format = len.toFixed(2) + "MB";
        break;
      case "GB":
        format = len.toFixed(2) + "GB";
        break;
      case "TB":
        format = len.toFixed(2) + "TB";
        break;
    }
    return format;
  }
}

let u = {
  el: '',
  id: '',
  name: '',
  type: '',
  classname: '',
  width: '',
  height: '',
}


class cps {
  constructor(data) {
    this.el = null
    let model = {}

    if (data.el) {
      this.el = data.el
      delete data.el
    }


  }

  bindModel(tar) {
    for (let eachKey in tar) {
      Object.defineProperty(this.model, eachKey, {
        set: (val) => {
          this.model[eachKey] = val
        }
      })
    }
  }
}


module.exports = (() => {
  if (global.cps) {
    return global.cps
  }

  global.cps = {
    $, //通过 # . 元素名 获取对应的html元素
    isArray, //判断是否数组
    print, //直接打印目标对象，如果是数组，自动遍历
    addElement, //创建元素
    interval, //创建定时器
    mergeArray, //合并数组
    getFileSize, //获取文件大小
    cps,
  }
  return global.cps
})()